<script lang="ts" setup>
import { reactive } from 'vue'
// 1. 根据后台需要的参数定义数据对象
// 2. 根据数据对象，绑定组件（复选框，排序按钮）
// 3. 在操作排序组件的时候，需要反馈给数据对象
// sortField====>publishTime,orderNum,price,evaluateNum
// sortMethod====>asc为正序 desc为倒序
type SortParamsType = {
  inventory: boolean | null
  onlyDiscount: boolean | null
  sortField: null | string
  sortMethod: null | string
}
const sortParams: SortParamsType = reactive({
  inventory: false,
  onlyDiscount: false,
  sortField: null,
  sortMethod: null
})
const emit = defineEmits<{
  (el: 'sort-change', value: SortParamsType): void
}>()
// 改变排序
const changeSort = (sortField: string | null | boolean) => {
  // 判断是否点击的价格排序按钮
  if (sortField === 'price') {
    sortParams.sortField = sortField
    if (sortParams.sortMethod === null) {
      // 如果是第一次点击，默认是降序
      sortParams.sortMethod = 'desc'
    } else {
      // 其他情况根据当前排序取反
      sortParams.sortMethod =
        sortParams.sortMethod === 'desc' ? 'asc' : 'desc'
    }
  } else {
    // 点击其他排序
    // 点击同个排序多次，跳出
    if (sortParams.sortField === sortField) return
    sortParams.sortField = sortField as string
    // 每次重新给价格排序改成初始状态
    sortParams.sortMethod = null
  }
  // 触发通知父组件事件改变了
  emit('sort-change', sortParams)
}
// 点击复选框触发
const changeCheck = () => {
  // 触发通知父组件事件改变了
  emit('sort-change', sortParams)
}
</script>

<template>
  <div class="sub-sort">
    <div class="sort">
      <a
        :class="{ active: sortParams.sortField === null }"
        @click="changeSort(null)"
        href="javascript:;"
        >默认排序</a
      >
      <a
        :class="{ active: sortParams.sortField === 'publishTime' }"
        @click="changeSort('publishTime')"
        href="javascript:;"
        >最新商品</a
      >
      <a
        :class="{ active: sortParams.sortField === 'orderNum' }"
        @click="changeSort('orderNum')"
        href="javascript:;"
        >最高人气</a
      >
      <a
        :class="{ active: sortParams.sortField === 'evaluateNum' }"
        @click="changeSort('evaluateNum')"
        href="javascript:;"
        >评论最多</a
      >
      <a @click="changeSort('price')" href="javascript:;">
        价格排序
        <i
          class="arrow up"
          :class="{
            active:
              sortParams.sortField === 'price' &&
              sortParams.sortMethod == 'asc'
          }"
        />
        <i
          class="arrow down"
          :class="{
            active:
              sortParams.sortField === 'price' &&
              sortParams.sortMethod == 'desc'
          }"
        />
      </a>
    </div>
    <div class="check">
      <XtxCheckbox v-model="sortParams.inventory" @change="changeCheck"
        >仅显示有货商品</XtxCheckbox
      >
      <XtxCheckbox v-model="sortParams.onlyDiscount" @change="changeCheck"
        >仅显示特惠商品</XtxCheckbox
      >
    </div>
  </div>
</template>

<style scoped lang="less">
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all 0.3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: @xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>
